package com.lee.batislogging;

import javax.sql.DataSource;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import com.lee.batislogging.entities.Student;
import com.lee.batislogging.mappers.StudentMapper;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
		// 纯代码方式访问
		DataSource dataSource = new PooledDataSource("com.mysql.cj.jdbc.Driver",
				"jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2b8",
				"root", "1234");
		TransactionFactory transactionFactory = new JdbcTransactionFactory();
		Environment environment = new Environment("development", transactionFactory, dataSource);
		Configuration configuration = new Configuration(environment);
		configuration.addMapper(StudentMapper.class);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
		try (SqlSession session = sqlSessionFactory.openSession()) {
			Student student = session.getMapper(StudentMapper.class).selectStudent(1);
			System.out.println(student);
		}
    }
}
